cmac 0.2.0

Generic implementation of Cipher-based Message Authentication Code
Documentation

Generic implementation of Cipher-based Message Authentication Code (CMAC), otherwise known as OMAC1.

Usage

We will use AES-128 block cipher from aes crate.

To get the authentication code:

extern crate cmac;
extern crate aes;

use aes::Aes128;
use cmac::{Cmac, Mac};

# fn main() {
// Create `Mac` trait implementation, namely CMAC-AES128
let mut mac = Cmac::<Aes128>::new_varkey(b"very secret key.").unwrap();
mac.input(b"input message");

// `result` has type `MacResult` which is a thin wrapper around array of
// bytes for providing constant time equality check
let result = mac.result();
// To get underlying array use `code` method, but be carefull, since
// incorrect use of the code value may permit timing attacks which defeat
// the security provided by the `MacResult`
let code_bytes = result.code();
# }

To verify the message:

# extern crate cmac;
# extern crate aes;
# use aes::Aes128;
# use cmac::{Cmac, Mac};
# fn main() {
let mut mac = Cmac::<Aes128>::new_varkey(b"very secret key.").unwrap();

mac.input(b"input message");

# let code_bytes = mac.clone().result().code();
// `verify` will return `Ok(())` if code is correct, `Err(MacError)` otherwise
mac.verify(&code_bytes).unwrap();
# }